/*
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-06-09 09:49:40
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-08-06 11:16:08
 * @FilePath: \linePatrol\vue-element-plus-admin\src\store\modules\plangroup.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import { store } from '../index'
import EquipTypeApi from '@/api/equip/equipType'
import {
  FacilityTypeList,
  FacilityTypeTreeData,
  facOutLedgerType,
  PnTopologyTreeType,
  ProducerLedgerType
} from '@/api/equip/type'
import PipnentInfoApi from '@/api/equip/pipnet'
import ProducerLedgerApi from '@/api/equip/producerLedger'
import FacOutLedgerApi from '@/api/equip/facOutLedger'
import { filterUndefined } from '@/utils/index'
export const useEquipStore = defineStore('equipstore', {
  state: () => {
    return {
      FacilityTypeDetail: {} as unknown as FacilityTypeList,
      FacilityTypeTree: {} as unknown as FacilityTypeTreeData[],
      PnTopologyDetail: {} as unknown as PnTopologyTreeType,
      ProducerLedgerList: [] as unknown as ProducerLedgerType[], //厂商
      FcaListOut: [] as unknown as facOutLedgerType[], //设施出厂列表，
      FacilityById: {} as unknown as FacilityTypeList,
      FacilityByFacId: { formschema: [], current: {} } as unknown as any,
      EquipFormData: {} as any
    }
  },
  getters: {
    getEquipFormData(): any {
      return this.EquipFormData
    },
    getFacilityTypeDetail(): FacilityTypeList {
      return this.FacilityTypeDetail
    },
    getFacilityTypeTree(): FacilityTypeTreeData[] {
      return this.FacilityTypeTree
    },
    getPnTopologyDetail(): PnTopologyTreeType {
      return this.PnTopologyDetail
    },
    getProducerLedger(): ProducerLedgerType[] {
      return this.ProducerLedgerList
    },
    getFcaListOut(): facOutLedgerType[] {
      return this.FcaListOut
    },
    getFacilityById(): FacilityTypeList {
      return this.FacilityById
    },
    getFacilityByFacId(): any {
      return this.FacilityByFacId
    }
  },
  actions: {
    async setFacilityTypeDetail(faciId: number | undefined) {
      this.FacilityTypeDetail = await EquipTypeApi.GetFacilityTypeDetail(faciId)
    },
    async setFacilityTypeTree() {
      this.FacilityTypeTree = await EquipTypeApi.GetFacilityTypeTree()
    },
    async setPnTopologyDetail(id: string) {
      this.PnTopologyDetail = await PipnentInfoApi.GetPnTopologyDetail(id)
    },
    async setProducerLedger() {
      this.ProducerLedgerList = (await ProducerLedgerApi.GetProducerLedger({})).rows
    },
    async setFcaListOut() {
      this.FcaListOut = (await FacOutLedgerApi.GetfacOutLedgerFacList({})).rows
    },
    async setFacilityById(id: number | undefined) {
      this.FacilityById = await EquipTypeApi.GetFacilityById(id)
    },
    setFacilityByTemplate(template: string | undefined) {
      if (template) {
        const formschema = JSON.parse(template).drawingList.map((item) => {
          return {
            component: item.component,
            field: item.vModel,
            hidden: false,
            label: item.label,
            // value:item.defaultValue?item.defaultValue:'',
            componentProps: filterUndefined({
              disabled: item.disabled,
              readonly: item.readonly,
              type: item.type,
              options: item.options,
              format: item.format,
              clearable: item.clearable,
              placeholder: item.placeholder,
              rangeSeparator: item['range-separator'],
              endPlaceholder: item['end-placeholder'],
              startPlaceholder: item['start-placeholder'],
              valueFormat: item['value-format']
            }),
            colProps: {
              span: item.span
            }
          }
        })
        const current = Object.fromEntries(
          JSON.parse(template).drawingList.map((item) => [item.vModel, item.defaultValue])
        )
        return { formschema: formschema, template: JSON.parse(template), current: current }
      }
    },
    async setFacilityByFacId(id: number | undefined) {
      this.FacilityByFacId = { formschema: [], current: {} }
      const i = await EquipTypeApi.GetFacilityByFaciId(id)
      if (i) {
        this.FacilityByFacId = this.setFacilityByTemplate(i?.template)
      }
    },
    setEquipFormData(data: any) {
      this.EquipFormData = data
    }
  },
  persist: true
})

export const useLockStoreWithOut = () => {
  return useEquipStore(store)
}
